package com.smh.demo1.a11;

import org.springframework.cglib.core.Signature;

/**
 * @author shiminghui
 * @date 2025/4/16 17:35
 * @description: TODO
 */
//这个类是在MethodProxy创建后第一次invoke时生成的,是动态生成的,在第一次invoke时会获取到index,保存,后序使用
// 内部会存储 FastClass的引用
public class TargetFastClass {

    static Signature readSignature = new Signature("read", "()V");

    public static void main(String[] args) {
        TargetFastClass targetFastClass = new TargetFastClass();
        int index = targetFastClass.getIndex(new Signature("read", "()V"));
        try {
            targetFastClass.invoke(index, new Target(), new Object[]{3});
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public int getIndex(Signature signature) {
        if (readSignature.equals(signature)) {
            return 0;
        } else {
            return -1;
        }
    }

    public Object invoke(int index, Object target, Object[] args) throws Throwable {
        if (index == 0) {
            ((Target) target).read((int) args[0]);
        }
        return null;
    }

}
